package 复习第二天;

import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Test {
    public boolean isValidSudoku(char[][] board) {
        //判断数独
        //创建三个数组
        int [][]row=new int[9][9];
        int [][]col=new int[9][9];
        //定义一个三维数组用来记录小九宫格只能出现一个数
        int[][][]subBoxes=new int[3][3][9];
        //遍历数组
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c=board[i][j];
                int index=0;
                if(c!='.') {
                     index=c-'0'-1;
                    row[i][index]++;
                    col[j][index]++;
                    subBoxes[i/3][j/3][index]++;
                }
                if(row[i][index]>1||col[j][index]>1||subBoxes[i/3][j/3][index]>1) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main1(String[] args) {
        //测试
        int[][]array= {{0, 1, 2, 0}, {3, 4, 5, 2},{1, 3, 1, 5}};
        setZeroes(array);
    }
    public static void setZeroes(int[][] matrix) {
        /**
         * 遍历二维数组，找到0之后就将它的行和列改为0
         */
        int[][]array=new int[matrix.length][matrix[0].length];
        for (int i = 0; i <matrix.length ; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if(matrix[i][j]==0) {
                    //行
                    for (int k = 0; k < matrix.length; k++) {
                        array[k][j]=0;
                    }
                    //列
                    for (int k = 0; k <matrix[0].length ; k++) {
                        array[i][k]=0;
                    }
                }else if(array[i][j]!=0){
                    array[i][j]=matrix[i][j];
                }
            }
        }
        for (int i = 0; i <matrix.length ; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                matrix[i][j]=array[i][j];
            }
        }
    }

    /**
     * 合并两个有序数组
     */
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int x=m-1;
        int y=n-1;
        int size=nums1.length-1;
        while (x>0&&y>0) {
            if(nums1[x]<nums2[y]) {
                nums1[size--]=nums2[y--];
            }else {
                nums1[size--]=nums1[x--];
            }
        }
        while (y>0) {
            nums1[size--]=nums2[y--];
        }
    }

    /**
     * 删除有序数组中的重复项
     */
    public int removeDuplicates1(int[] nums) {
        int p1=0;
        int p2=1;
        while (p2!=nums.length) {
            if (nums[p1] != nums[p2]) {
                p1++;
                nums[p1] = nums[p2];
            }
            p2++;
        }
        return p1+1;
    }

    public static void main2(String[] args) {
        int[]array={0,0,1,1,1,1,2,3,3};
        removeDuplicates(array);
    }
    public static int removeDuplicates(int[] nums) {
      //  LinkedList<Integer> list=new LinkedList<>();
        int count=0;//计数
        int slow=2;
        int fast=2;
        int size=nums.length;
        if(size<=2) {
            return size;
        }
        while (fast<size) {
            //如果相等则至少有三个相同的元素
            if(nums[slow-2]!=nums[fast]) {
                nums[slow]=nums[fast];
                slow++;
            }
            fast++;
        }
        return slow;

    }

    public static void main3(String[] args) {
        //字符集合
        //创建数组
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str=scanner.nextLine();
            int[]array=new int[130];
         //   int[]newArray=new int[130];
            for (int i = 0; i < str.length(); i++) {
                char c=str.charAt(i);
                array[c]++;
            }
            for (int i = 0; i <str.length(); i++) {
                char c=str.charAt(i);
                if(array[c]!=0) {
                    array[c]=0;
                    System.out.print(c);
                }
            }
        }
    }
    public static void main(String[] args) {
        //字符集合
        //创建数组
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str=scanner.nextLine();
            boolean[]array=new boolean[130];
            StringBuilder s=new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
                char c=str.charAt(i);
                if(!array[c]) {
                    s.append(c);
                    array[c]=true;
                }
            }
            System.out.println(s.toString());
        }
    }
    public int firstUniqChar(String s) {
        Map<Character,Integer> map = new TreeMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);
        }
        for (int i = 0; i < s.length(); i++) {
            if(map.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
/*        int[] array =new int[27];
        for(int i = 0; i < s.length();i++) {
            array[s.charAt(i)-'a']++;
        }
        for (int i = 0; i < array.length; i++) {
            if(array[s.charAt(i)-'a'] == 1) {
                return s.charAt(i);
            }
        }
    return -1;*/
    }
}








